

#include "em_common_i2c.h"


#include "app_i2c.h"
#include "boards.h"

static bool common_i2c_is_initialized = false;

void em_common_i2c_init(void)
{
    if( common_i2c_is_initialized ) 
    {
        return;
    }
    
    common_i2c_is_initialized = true; 
    
    app_i2c_params_t master_params = 
    {
        .id = COMMON_I2C_ID,
        .role = APP_I2C_ROLE_MASTER,
        .pin_cfg = 
        {
            .scl = 
            {
                .type = COMMON_SCL_PORT,
                .mux = COMMON_SCL_MUX,
                .pin = COMMON_SCL_PIN,
                .pull = APP_IO_PULLUP,
            },
            .sda = 
            {
                .type = COMMON_SDA_PORT,
                .mux = COMMON_SDA_MUX,
                .pin = COMMON_SDA_PIN,
                .pull = APP_IO_PULLUP
            },
        },
        .use_mode = 
        {
            .type = APP_I2C_TYPE_POLLING,
        },
        .init = 
        {
            .speed = I2C_SPEED_400K,
            .own_address = 0x00,
            .addressing_mode = I2C_ADDRESSINGMODE_7BIT ,
            .general_call_mode = I2C_GENERALCALL_DISABLE
        }
    };
    
    app_drv_err_t ret = app_i2c_init( &master_params, NULL );
    if( ret != APP_DRV_SUCCESS )
    {
        
    }
    
}

void em_common_i2c_unit(void)
{
    app_i2c_deinit( COMMON_I2C_ID  );
    common_i2c_is_initialized = false; 
}
